package org.shangrila.app.sys.mb;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;
import org.primefaces.event.UnselectEvent;
import org.shangrila.app.sys.eo.SysAuthority;
import org.shangrila.app.sys.eo.SysRole;
import org.shangrila.comm.dao.DbAccess;
import org.shangrila.comm.utils.MsgsUtil;
 
@Named
@SessionScoped
public class SysRoleMB implements Serializable {
	private static final long serialVersionUID = -5951492023664217108L;
	private List<SysRole> voSysRole; //显示的角色列表
    private SysRole sysRole;
    private SysRole selectedRole;
    
	private String rolename;	
	private List<SysAuthority> voSetted=new ArrayList<SysAuthority>();		//当前记录已经拥有的记录集合
	private List<SysAuthority> voSelections=new ArrayList<SysAuthority>();	//可以选择设置的记录集合
	private List<SysAuthority> voSelected=new ArrayList<SysAuthority>();    //选择的权限
	private List<SysAuthority> selectedAuths=new ArrayList<SysAuthority>();    //选择后等待设置的权限
	private List<SysAuthority> voAuthorities=new ArrayList<SysAuthority>(); //所有权限集合

    private DbAccess db = new DbAccess();
    
    @PostConstruct
    public void init() {
    	newSysRole();
    	voSysRole = db.findEntityAll("SysRole");
    	voAuthorities = db.findEntityAll("SysAuthority");
    }
    public void refresh() {
    	voSysRole = db.findEntityAll("SysRole");
    	voAuthorities = db.findEntityAll("SysAuthority");
    	MsgsUtil.showMessages("数据刷新", "成功");
    }
         
    public void newSysRole() {
		sysRole = new SysRole();
		sysRole.setFlag("1");
    }

    public void onTabChange(TabChangeEvent event) {
    	String tabid =  event.getTab().getId();    	
    	if("tab2".equals(tabid)) {    		
    		selectedRole = voSysRole.get(0);
    		doRoleRowSelect(); 
    	}
    }
    
	public String addAction() {
		
		db.beginTransaction();
		db.insert(sysRole);
		db.commitTransaction();
		
		addMessage("成功", "信息录入成功");
		
		voSysRole.add(sysRole);
		newSysRole();
		return null;
	}
      
    public void onRowEdit(RowEditEvent event) {
    	selectedRole = voSysRole.get(voSysRole.indexOf((SysRole) event.getObject()));
    	db.beginTransaction();
    	db.update(selectedRole);
    	db.commitTransaction();
    	MsgsUtil.showMessages("编辑", "编辑保存成功。");
    }
     
    public void onRowCancel(RowEditEvent event) {
    	MsgsUtil.showMessages("编辑", "取消编辑成功。");
    }
     
	public String authAction(){
		voSetted.addAll(selectedAuths);//把新的权限添加到已有权限列表
     	
		//System.out.println("voSelected2:"+voSelected.size());
		//voSetted.addAll(voSelected);//把新的权限添加到已有权限列表  :不知为什么突然不行了
    	//System.out.println("voSetted2:"+voSetted.size()); 
    	
		selectedRole.setSysAuthorities(voSetted);
		db.beginTransaction();
		db.update(selectedRole);
		db.commitTransaction();	
		
		voSelections.removeAll(selectedAuths);
		MsgsUtil.showMessages("授权", "授予选定权限成功。");
		return null;
	}
	
	public String revokeAction(){		
		voSetted.removeAll(voSelected);//把新的权限添加到已有权限列表		
		selectedRole.setSysAuthorities(voSetted);
		db.beginTransaction();
		db.update(selectedRole);
		db.commitTransaction();	
		voSelections.addAll(voSelected);
		MsgsUtil.showMessages("授权", "回收选定权限成功。");
		return null;
	}	
	
    public void onAuthRowSelect(SelectEvent event) {
    	selectedAuths.clear();
    	selectedAuths.addAll(voSelected);
     }
 
    public void onAuthRowUnselect(UnselectEvent event) {
    	//MsgsUtil.showMessages("Authority Unselected", ((SysAuthority) event.getObject()).getName());
    }
    public void onAuthoredRowSelect(SelectEvent event) {
    	//MsgsUtil.showMessages("Authority Selected", ((SysAuthority) event.getObject()).getName());
     }
 
    public void onAuthoredRowUnselect(UnselectEvent event) {
    	//MsgsUtil.showMessages("Authority Unselected", ((SysAuthority) event.getObject()).getName());
    }

    //角色行选择
    public void onRoleRowSelect(SelectEvent event) {
    	selectedRole = (SysRole) event.getObject();
    	doRoleRowSelect();   	
     }
    
    public void doRoleRowSelect() {
    	rolename = selectedRole.getName();    
    	voSetted = selectedRole.getSysAuthorities();
    	voSelections = new ArrayList<SysAuthority>();

    	voSelections.removeAll(voAuthorities);
    	voSelections.addAll(voAuthorities);

		//移除已经拥有的权限
		Iterator<SysAuthority> iter = voSelections.iterator();  
		while(iter.hasNext()){
			if(voSetted.contains(iter.next()))
		        iter.remove();  
		}  	
     }
    
    public void onRoleRowUnselect(UnselectEvent event) {
    	//MsgsUtil.showMessages("Authority Unselected", ((SysRole) event.getObject()).getUsername());
    }
    
	public void addMessage(String summary, String detail) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,summary, detail);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public List<SysRole> getVoSysRole() {
		return voSysRole;
	}

	public void setVoSysRole(List<SysRole> voSysRole) {
		this.voSysRole = voSysRole;
	}

	public SysRole getSysRole() {
		return sysRole;
	}

	public void setSysRole(SysRole sysRole) {
		this.sysRole = sysRole;
	}

	public SysRole getSelectedRole() {
		return selectedRole;
	}

	public void setSelectedRole(SysRole selectedRole) {
		this.selectedRole = selectedRole;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	public List<SysAuthority> getVoSetted() {
		return voSetted;
	}

	public void setVoSetted(List<SysAuthority> voSetted) {
		this.voSetted = voSetted;
	}

	public List<SysAuthority> getVoSelections() {
		return voSelections;
	}

	public void setVoSelections(List<SysAuthority> voSelections) {
		this.voSelections = voSelections;
	}

	public List<SysAuthority> getVoSelected() {
		return voSelected;
	}

	public void setVoSelected(List<SysAuthority> voSelected) {
		this.voSelected = voSelected;
	}

	public List<SysAuthority> getSelectedAuths() {
		return selectedAuths;
	}

	public void setSelectedAuths(List<SysAuthority> selectedAuths) {
		this.selectedAuths = selectedAuths;
	}

	public List<SysAuthority> getVoAuthorities() {
		return voAuthorities;
	}

	public void setVoAuthorities(List<SysAuthority> voAuthorities) {
		this.voAuthorities = voAuthorities;
	}

}